package com.msti.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.DateFormatter;

import com.msti.entity.Batch;
import com.msti.entity.Course;
import com.msti.service.BatchMgtDAO;
import com.msti.service.BatchMgtDAOImpl;
import com.msti.service.CourseMgtDAO;
import com.msti.service.CourseMgtDAOImpl;
import com.msti.utility.JDBCCon;
import com.msti.utility.Utility;
/**
 * Servlet implementation class CreateBatch
 */
public class CreateBatch extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private final static String FILE_NAME = "C:/Res/Files/btf.dat";  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CreateBatch() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stu
		String courseName = request.getParameter("course");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		CourseMgtDAO courseMgtDAO = new CourseMgtDAOImpl();
		Course course = courseMgtDAO.getCourseByName(courseName);
		Batch batch = new Batch();
		int tempId = Utility.getId(FILE_NAME);
		String batchId = Utility.getBatchId(tempId);
		batch.setBatchId(batchId);
		batch .setCompleatedDate(Utility.stringToDate(endDate));
		batch.setCourse(course);
		batch.setStartDate(Utility.stringToDate(startDate));
		Utility.writeId(tempId, FILE_NAME);
		//batch.setStartDate(Utility.stringToDate(startDate));
		//batch.setCompleatedDate(Utility.stringToDate(endDate));
		//batch.setCourse_id(course.getId());
		BatchMgtDAO batchMgtDAO = new BatchMgtDAOImpl();
		batchMgtDAO.createNewBatch(batch);
		String query = "update Batch set startDate='"+startDate+"' , compleatedDate='"+endDate+
		"' where id='"+batchId+"'";
		Connection con = JDBCCon.getConnection();
		Statement statement;
		try {
			statement = con.createStatement();
			statement.executeUpdate(query);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JDBCCon.closeConnection();
		response.sendRedirect("loadCourses.do");
	}
	
	

}
